package vip.mall.modular.user.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import vip.mall.common.result.CommonResult;
import vip.mall.modular.user.entity.User;
import vip.mall.modular.user.params.AddUserParam;
import vip.mall.modular.user.params.EditUserPasswordParams;
import vip.mall.modular.user.params.UserPageParams;
import vip.mall.modular.user.service.UserService;

/**
 * @author Asus
 * @Date 2025/2/10 10:13
 * @Descripition 用户模块描述
 */
@Tag(name = "用户模块控制器")
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 添加用户
     *
     * @param addUserParam 添加用户必填参数
     * @return 添加用户结果
     */
    @PostMapping("/add")
    @Operation(summary = "添加用户")
    public CommonResult addUser(@RequestBody @Validated AddUserParam addUserParam) {
        boolean result = userService.add(addUserParam);
        return result ? CommonResult.success() : CommonResult.error();
    }

    /**
     *  禁用用户
     * @param userId 用户ID
     * @return 禁用用户结果
     */

    @GetMapping("/disable")
    @Operation(summary = "禁用用户")
    public CommonResult disable(@RequestParam("id") String userId) {
        boolean result = userService.removeById(userId);
        return result ? CommonResult.success() : CommonResult.error();
    }

    /**
     *  删除用户
     * @param userId 用户ID
     * @return 删除用户结果
     */
    @GetMapping("/delete")
    @Operation(summary = "删除用户")
    public CommonResult delete(@RequestParam("id") String userId) {
        boolean result = userService.delete(userId);
        return result ? CommonResult.success() : CommonResult.error();
    }
    @PostMapping("/edit")
    @Operation(summary = "修改用户")
    public CommonResult edit(@RequestBody User user) {
        boolean result = userService.updateById(user);
        return result ? CommonResult.success() : CommonResult.error();
    }

    /**
     *  分页查询禁用用户
     * @param userPageParams 参数
     * @return 分页查询禁用用户结果
     */
    @PostMapping("/selectDeleteUser")
    @Operation(summary = "分页查询禁用用户")
    public CommonResult deleteUser(@RequestBody UserPageParams userPageParams) {
        return CommonResult.success(userService.selectDeleteUser(userPageParams));
    }
    /**
     * 获取用户列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取用户列表")
    public CommonResult list() {
        return CommonResult.success(userService.list());
    }

    /**
     *  分页获取用户列表
     * @param userPageParams 参数
     * @return 分页获取用户列表结果
     */
    @PostMapping("/page")
    @Operation(summary = "分页获取用户列表")
    public CommonResult page(@RequestBody UserPageParams userPageParams) {
        return CommonResult.success(userService.pageList(userPageParams));
    }
    /**
     *  分页获取用户列表
     * @param username 参数
     * @return 分页获取用户列表结果
     */
    @GetMapping("/userInfo")
    @Operation(summary = "根据登录用户名获取用户信息")
    public CommonResult getUserInfo(@RequestParam ("username") String username) {
        return CommonResult.success(userService.getUserInfo(username));
    }

    @PostMapping("/editPassword")
    @Operation(summary = "修改密码")
    public CommonResult updatePassword(@RequestBody @Validated EditUserPasswordParams editUserPasswordParams){
        boolean editedUserPassword = userService.editUserPassword(editUserPasswordParams);
        return editedUserPassword?CommonResult.success():CommonResult.error("原密码错误");
    }
}
